package com.ray.scan;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ImageView;

public class DrawImageView extends ImageView{
    
    public static final int DRAW_QR_CORD = 3;    // 绘制二维码扫描大小是根据屏幕宽度除以3
    public static final int DRAW_TAKE_ICON = 5;    // 绘制图片扫描大小是根据屏幕宽度除以5
    
    private int mWidth;
    private int mHeight;
    private int mSpe;
    
    /**
     * 
     * @param context
     * @param width
     * @param height
     * @param spe 规格
     */
    public DrawImageView(Context context, int width, int height, int spe){
        super(context);
        // TODO Auto-generated constructor stub
        mWidth = width;
        mHeight = height;
        mSpe= spe;
    }

	public DrawImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	
	Paint fillPaint = new Paint();
	{
	    fillPaint.setColor(Color.parseColor("#c0000000"));
	    fillPaint.setStyle(Paint.Style.FILL_AND_STROKE);
	}
	
	Paint paint = new Paint();
	{
		paint.setAntiAlias(true);
		paint.setColor(Color.parseColor("#FFF0F5"));
		paint.setStyle(Style.STROKE);
		paint.setStrokeWidth(1.0f);//设置线宽
		paint.setAlpha(100);
	};
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		int centerX = (mWidth / 2);
		int centerY = (mHeight / 2);
//		canvas.drawRect(new Rect(100, 200,
//                400, 500), paint);
		canvas.drawRect(new Rect(0, 0,
		        mWidth, (centerY - mSpe)), fillPaint);
        canvas.drawRect(new Rect(0, (centerY - mSpe),
                (centerX - mSpe), (centerY + mSpe)), fillPaint);
        canvas.drawRect(new Rect((centerX + mSpe), (centerY - mSpe),
                mWidth, (centerY + mSpe)), fillPaint);
        canvas.drawRect(new Rect(0, (centerY + mSpe),
                mWidth, mHeight), fillPaint);
        
        Paint p = new Paint();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qr_left_up);  
        canvas.drawBitmap(bitmap, (centerX - mSpe), (centerY - mSpe), p);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qr_right_up);  
        canvas.drawBitmap(bitmap, (centerX + mSpe - bitmap.getWidth()), (centerY - mSpe), p);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qr_left_down);  
        canvas.drawBitmap(bitmap, (centerX - mSpe), (centerY + mSpe - bitmap.getHeight()), p);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qr_right_down);  
        canvas.drawBitmap(bitmap, (centerX + mSpe - bitmap.getWidth()), (centerY + mSpe - bitmap.getHeight()), p);

        // 编写扫描框下面的文字
//        p.setTextSize(24);
//        p.setStyle(Paint.Style.FILL);    // 充满
//        p.setColor(Color.LTGRAY);
//        canvas.drawText("将二维码或图片放入框中保持三秒不动，则自动扫描", 
//                (centerX - mSpe), (centerY + mSpe + 50), p);
        Rect targetRect = new Rect(0, (centerY + mSpe + 20), mWidth, (centerY + mSpe + 80));  
//        p = new Paint(Paint.ANTI_ALIAS_FLAG);  
//        p.setStrokeWidth(3); 
//        p.setColor(Color.CYAN);  
//        canvas.drawRect(targetRect, paint); 
        p.setTextSize(20);
        String testString = "将二维码或图片放入框中保持三秒不动，则自动扫描";  
        p.setColor(Color.LTGRAY);  
        FontMetricsInt fontMetrics = p.getFontMetricsInt();  
        int baseline = targetRect.top + 
                (targetRect.bottom - targetRect.top - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;  
        // 下面这行是实现水平居中，drawText对应改为传入targetRect.centerX()  
        p.setTextAlign(Paint.Align.CENTER);  
        canvas.drawText(testString, targetRect.centerX(), baseline, p);
        
		canvas.drawRect(new Rect((centerX - mSpe), (centerY - mSpe),
		        (centerX + mSpe), (centerY + mSpe)), paint);    // 绘制矩形，前两个值是左上角点坐标，后两个值是右下角点坐标
	}
}
